

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/icon.png">
  <link rel="icon" href="/img/icon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="d-lue">
  <meta name="keywords" content="">
  
    <meta name="description" content="js中判断数据类型在JavaScript中，可以使用不同的方式来判断数据类型。以下是一些常见的方法：  使用typeof操作符： 使用typeof操作符可以快速判断一个值的数据类型，它返回一个字符串，表示该值的数据类型。常见的typeof结果包括：  “number”：数字 “string”：字符串 “boolean”：布尔值 “object”：对象（包括数组和null） “function”：函">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>JavaScript笔记（二） - d-lue&#39; blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"dl_coisini.gitee.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":"9c32ca7145bad202032b544e8bf27d19","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  

  
    <!-- Baidu Analytics -->
    <script async>
      if (!Fluid.ctx.dnt) {
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?9c32ca7145bad202032b544e8bf27d19";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
      }
    </script>
  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>d-lue&#39;s blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/bg/wenzhang.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="JavaScript笔记（二）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-09-19 00:00" pubdate>
          2023年9月19日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4.3k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          36 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">JavaScript笔记（二）</h1>
            
            
              <div class="markdown-body">
                
                <h3 id="js中判断数据类型"><a href="#js中判断数据类型" class="headerlink" title="js中判断数据类型"></a>js中判断数据类型</h3><p>在JavaScript中，可以使用不同的方式来判断数据类型。以下是一些常见的方法：</p>
<ol>
<li><p>使用typeof操作符： 使用typeof操作符可以快速判断一个值的数据类型，它返回一个字符串，表示该值的数据类型。常见的typeof结果包括：</p>
<ul>
<li>“number”：数字</li>
<li>“string”：字符串</li>
<li>“boolean”：布尔值</li>
<li>“object”：对象（包括数组和null）</li>
<li>“function”：函数</li>
<li>“undefined”：未定义</li>
</ul>
<p>示例：</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs awk">typeof <span class="hljs-number">42</span>; <span class="hljs-regexp">//</span> <span class="hljs-string">&quot;number&quot;</span><br>typeof <span class="hljs-string">&quot;Hello&quot;</span>; <span class="hljs-regexp">//</span> <span class="hljs-string">&quot;string&quot;</span><br>typeof true; <span class="hljs-regexp">//</span> <span class="hljs-string">&quot;boolean&quot;</span><br>typeof &#123; key: <span class="hljs-string">&quot;value&quot;</span> &#125;; <span class="hljs-regexp">//</span> <span class="hljs-string">&quot;object&quot;</span><br>typeof <span class="hljs-keyword">function</span>() &#123;&#125;; <span class="hljs-regexp">//</span> <span class="hljs-string">&quot;function&quot;</span><br>typeof undefined; <span class="hljs-regexp">//</span> <span class="hljs-string">&quot;undefined&quot;</span><br></code></pre></td></tr></table></figure>
</li>
<li><p>使用instanceof操作符： instanceof操作符用于检查对象是否是某个构造函数的实例。它可以用来判断一个值是否属于特定的对象类型。</p>
<p>示例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> arr = [];<br><span class="hljs-keyword">var</span> obj = &#123;&#125;;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Array</span>); <span class="hljs-comment">// true</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Object</span>); <span class="hljs-comment">// true</span><br></code></pre></td></tr></table></figure>
</li>
<li><p>使用Object.prototype.toString.call()方法： 这种方法可以精确地判断数据类型，包括内置对象和自定义对象。</p>
<p>示例：</p>
<figure class="highlight sqf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs sqf"><span class="hljs-built_in">Object</span>.prototype.<span class="hljs-built_in">toString</span>.<span class="hljs-built_in">call</span>(<span class="hljs-number">42</span>); <span class="hljs-comment">// &quot;[object Number]&quot;</span><br><span class="hljs-built_in">Object</span>.prototype.<span class="hljs-built_in">toString</span>.<span class="hljs-built_in">call</span>(<span class="hljs-string">&quot;Hello&quot;</span>); <span class="hljs-comment">// &quot;[object String]&quot;</span><br><span class="hljs-built_in">Object</span>.prototype.<span class="hljs-built_in">toString</span>.<span class="hljs-built_in">call</span>(<span class="hljs-literal">true</span>); <span class="hljs-comment">// &quot;[object Boolean]&quot;</span><br><span class="hljs-built_in">Object</span>.prototype.<span class="hljs-built_in">toString</span>.<span class="hljs-built_in">call</span>(&#123; key: <span class="hljs-string">&quot;value&quot;</span> &#125;); <span class="hljs-comment">// &quot;[object Object]&quot;</span><br><span class="hljs-built_in">Object</span>.prototype.<span class="hljs-built_in">toString</span>.<span class="hljs-built_in">call</span>(function() &#123;&#125;); <span class="hljs-comment">// &quot;[object Function]&quot;</span><br></code></pre></td></tr></table></figure>
</li>
<li><p>使用typeof和constructor组合： 您可以使用typeof和对象的constructor属性来进一步确定对象的类型。</p>
<p>示例：</p>
<figure class="highlight arcade"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs arcade"><span class="hljs-keyword">var</span> arr = [];<br><span class="hljs-keyword">var</span> obj = &#123;&#125;;<br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-built_in">typeof</span> arr); <span class="hljs-comment">// &quot;object&quot;</span><br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(arr.constructor === <span class="hljs-built_in">Array</span>); <span class="hljs-comment">// true</span><br><br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-built_in">typeof</span> obj); <span class="hljs-comment">// &quot;object&quot;</span><br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(obj.constructor === Object); <span class="hljs-comment">// true</span><br></code></pre></td></tr></table></figure></li>
</ol>
<hr>
<h3 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h3><p>有三种常用的类型转换：转换为 string 类型、转换为 number 类型和转换为 boolean 类型。</p>
<p><strong>字符串转换</strong> —— 转换发生在输出内容的时候，也可以通过 <code>String(value)</code> 进行显式转换。原始类型值的 string 类型转换通常是很明显的。</p>
<p><strong>数字型转换</strong> —— 转换发生在进行算术操作时，也可以通过 <code>Number(value)</code> 进行显式转换。</p>
<p>数字型转换遵循以下规则：</p>
<table>
<thead>
<tr>
<th align="left">值</th>
<th align="left">变成……</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>undefined</code></td>
<td align="left"><code>NaN</code></td>
</tr>
<tr>
<td align="left"><code>null</code></td>
<td align="left"><code>0</code></td>
</tr>
<tr>
<td align="left"><code>true / false</code></td>
<td align="left"><code>1 / 0</code></td>
</tr>
<tr>
<td align="left"><code>string</code></td>
<td align="left">“按原样读取”字符串，两端的空白字符（空格、换行符 <code>\n</code>、制表符 <code>\t</code> 等）会被忽略。空字符串变成 <code>0</code>。转换出错则输出 <code>NaN</code>。</td>
</tr>
</tbody></table>
<p><strong>布尔型转换</strong> —— 转换发生在进行逻辑操作时，也可以通过 <code>Boolean(value)</code> 进行显式转换。</p>
<p>布尔型转换遵循以下规则：</p>
<table>
<thead>
<tr>
<th align="left">值</th>
<th align="left">变成……</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>0</code>, <code>null</code>, <code>undefined</code>, <code>NaN</code>, <code>&quot;&quot;</code></td>
<td align="left"><code>false</code></td>
</tr>
<tr>
<td align="left">其他值</td>
<td align="left"><code>true</code></td>
</tr>
</tbody></table>
<p>上述的大多数规则都容易理解和记忆。人们通常会犯错误的值得注意的例子有以下几个：</p>
<ul>
<li>对 <code>undefined</code> 进行数字型转换时，输出结果为 <code>NaN</code>，而非 <code>0</code>。</li>
<li>对 <code>&quot;0&quot;</code> 和只有空格的字符串（比如：<code>&quot; &quot;</code>）进行布尔型转换时，输出结果为 <code>true</code>。</li>
</ul>
<hr>
<h3 id="值的比较"><a href="#值的比较" class="headerlink" title="值的比较"></a>值的比较</h3><ul>
<li>比较运算符始终返回布尔值。</li>
<li>字符串的比较，会按照“词典”顺序逐字符地比较大小。</li>
<li>当对不同类型的值进行比较时，它们会先被转化为数字（不包括严格相等检查）再进行比较。</li>
<li>在非严格相等 <code>==</code> 下，<code>null</code> 和 <code>undefined</code> 相等且各自不等于任何其他的值。</li>
<li>在使用 <code>&gt;</code> 或 <code>&lt;</code> 进行比较时，需要注意变量可能为 <code>null/undefined</code> 的情况。比较好的方法是单独检查变量是否等于 <code>null/undefined</code>。</li>
</ul>
<hr>
<h3 id="数组的方法"><a href="#数组的方法" class="headerlink" title="数组的方法"></a>数组的方法</h3><p>数组方法备忘单：</p>
<ul>
<li>添加&#x2F;删除元素：<ul>
<li><code>push(...items)</code> —— 向尾端添加元素，</li>
<li><code>pop()</code> —— 从尾端提取一个元素，</li>
<li><code>shift()</code> —— 从首端提取一个元素，</li>
<li><code>unshift(...items)</code> —— 向首端添加元素，</li>
<li><code>splice(pos, deleteCount, ...items)</code> —— 从 <code>pos</code> 开始删除 <code>deleteCount</code> 个元素，并插入 <code>items</code>。</li>
<li><code>slice(start, end)</code> —— 创建一个新数组，将从索引 <code>start</code> 到索引 <code>end</code>（但不包括 <code>end</code>）的元素复制进去。</li>
<li><code>concat(...items)</code> —— 返回一个新数组：复制当前数组的所有元素，并向其中添加 <code>items</code>。如果 <code>items</code> 中的任意一项是一个数组，那么就取其元素。</li>
</ul>
</li>
<li>搜索元素：<ul>
<li><code>indexOf/lastIndexOf(item, pos)</code> —— 从索引 <code>pos</code> 开始搜索 <code>item</code>，搜索到则返回该项的索引，否则返回 <code>-1</code>。</li>
<li><code>includes(value)</code> —— 如果数组有 <code>value</code>，则返回 <code>true</code>，否则返回 <code>false</code>。</li>
<li><code>find/filter(func)</code> —— 通过 <code>func</code> 过滤元素，返回使 <code>func</code> 返回 <code>true</code> 的第一个值&#x2F;所有值。</li>
<li><code>findIndex</code> 和 <code>find</code> 类似，但返回索引而不是值。</li>
</ul>
</li>
<li>遍历元素：<ul>
<li><code>forEach(func)</code> —— 对每个元素都调用 <code>func</code>，不返回任何内容。</li>
</ul>
</li>
<li>转换数组：<ul>
<li><code>map(func)</code> —— 根据对每个元素调用 <code>func</code> 的结果创建一个新数组。</li>
<li><code>sort(func)</code> —— 对数组进行原位（in-place）排序，然后返回它。</li>
<li><code>reverse()</code> —— 原位（in-place）反转数组，然后返回它。</li>
<li><code>split/join</code> —— 将字符串转换为数组并返回。</li>
<li><code>reduce/reduceRight(func, initial)</code> —— 通过对每个元素调用 <code>func</code> 计算数组上的单个值，并在调用之间传递中间结果。</li>
</ul>
</li>
<li>其他：<ul>
<li><code>Array.isArray(value)</code> 检查 <code>value</code> 是否是一个数组，如果是则返回 <code>true</code>，否则返回 <code>false</code>。</li>
</ul>
</li>
</ul>
<p>请注意，<code>sort</code>，<code>reverse</code> 和 <code>splice</code> 方法修改的是数组本身。</p>
<p><code>arr.some(fn)、arr.every(fn) </code>检查数组。</p>
<p>与 <code>map</code> 类似，对数组的每个元素调用函数 <code>fn</code>。如果任何&#x2F;所有结果为 <code>true</code>，则返回 <code>true</code>，否则返回 <code>false</code>。</p>
<p>这两个方法的行为类似于 <code>||</code> 和 <code>&amp;&amp;</code> 运算符：如果 <code>fn</code> 返回一个真值，<code>arr.some()</code> 立即返回 <code>true</code> 并停止迭代其余数组项；如果 <code>fn</code> 返回一个假值，<code>arr.every()</code> 立即返回 <code>false</code> 并停止对其余数组项的迭代。</p>
<p>我们可以使用 <code>every</code> 来比较数组：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">arraysEqual</span>(<span class="hljs-params">arr1, arr2</span>) &#123;<br>  <span class="hljs-keyword">return</span> arr1.<span class="hljs-property">length</span> === arr2.<span class="hljs-property">length</span> &amp;&amp; arr1.<span class="hljs-title function_">every</span>(<span class="hljs-function">(<span class="hljs-params">value, index</span>) =&gt;</span> value === arr2[index]);<br>&#125;<br><br><span class="hljs-title function_">alert</span>( <span class="hljs-title function_">arraysEqual</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>])); <span class="hljs-comment">// true</span><br></code></pre></td></tr></table></figure>

<hr>
<h3 id="解构赋值"><a href="#解构赋值" class="headerlink" title="解构赋值"></a>解构赋值</h3><p>数组</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs awk">let [name: n = <span class="hljs-string">&quot;Guest&quot;</span>, surname: sn = <span class="hljs-string">&quot;Anonymous&quot;</span>] = [<span class="hljs-string">&quot;Julius&quot;</span>];<br><br>alert(n);    <span class="hljs-regexp">//</span> Julius（来自数组的值）<br>alert(sn); <span class="hljs-regexp">//</span> Anonymous（默认值被使用了）<br></code></pre></td></tr></table></figure>

<p>对象</p>
<figure class="highlight gauss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs gauss"><span class="hljs-keyword">let</span> options = &#123;<br>  <span class="hljs-built_in">title</span>: <span class="hljs-string">&quot;Menu&quot;</span><br>&#125;;<br><br><span class="hljs-keyword">let</span> &#123;width: w = <span class="hljs-number">100</span>, height: h = <span class="hljs-number">200</span>, <span class="hljs-built_in">title</span>&#125; = options;<br><br><span class="hljs-built_in">alert</span>(<span class="hljs-built_in">title</span>);  <span class="hljs-comment">// Menu</span><br><span class="hljs-built_in">alert</span>(w);      <span class="hljs-comment">// 100</span><br><span class="hljs-built_in">alert</span>(h);      <span class="hljs-comment">// 200</span><br></code></pre></td></tr></table></figure>

<p>嵌套结构</p>
<figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs arduino">let options = &#123;<br>  size: &#123;<br>    width: <span class="hljs-number">100</span>,<br>    height: <span class="hljs-number">200</span><br>  &#125;,<br>  items: [<span class="hljs-string">&quot;Cake&quot;</span>, <span class="hljs-string">&quot;Donut&quot;</span>],<br>  extra: <span class="hljs-literal">true</span><br>&#125;;<br><br><span class="hljs-comment">// 为了清晰起见，解构赋值语句被写成多行的形式</span><br>let &#123;<br>  size: &#123; <span class="hljs-comment">// 把 size 赋值到这里</span><br>    width,<br>    height<br>  &#125;,<br>  items: [item1, item2], <span class="hljs-comment">// 把 items 赋值到这里</span><br>  title = <span class="hljs-string">&quot;Menu&quot;</span> <span class="hljs-comment">// 在对象中不存在（使用默认值）</span><br>&#125; = options;<br><br><span class="hljs-built_in">alert</span>(title);  <span class="hljs-comment">// Menu</span><br><span class="hljs-built_in">alert</span>(width);  <span class="hljs-comment">// 100</span><br><span class="hljs-built_in">alert</span>(height); <span class="hljs-comment">// 200</span><br><span class="hljs-built_in">alert</span>(item1);  <span class="hljs-comment">// Cake</span><br><span class="hljs-built_in">alert</span>(item2);  <span class="hljs-comment">// Donut</span><br></code></pre></td></tr></table></figure>

<hr>
<h3 id="for-in、for-of"><a href="#for-in、for-of" class="headerlink" title="for in、for of"></a>for in、for of</h3><figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs rust"><span class="hljs-keyword">for</span> <span class="hljs-title class_">in</span>适合遍历对象，<br><span class="hljs-keyword">for</span> <span class="hljs-title class_">of</span>适合遍历数组。<br><span class="hljs-keyword">for</span> <span class="hljs-title class_">in</span>遍历的是数组的索引，对象的属性，以及原型链上的属性。<br><br>区别一：<span class="hljs-keyword">for</span> <span class="hljs-title class_">in</span> 和 <span class="hljs-keyword">for</span> <span class="hljs-title class_">of</span> 都可以循环数组。<span class="hljs-keyword">for</span> <span class="hljs-title class_">in</span> 输出的是数组的index下标，而<span class="hljs-keyword">for</span> <span class="hljs-title class_">of</span> 输出的是数组的每一项的值。<br>区别二：<span class="hljs-keyword">for</span> <span class="hljs-title class_">in</span> 可以遍历对象，<span class="hljs-keyword">for</span> <span class="hljs-title class_">of</span> 不能，只能遍历带有iterator接口的，例如Set,Map,<span class="hljs-type">String</span>,Array<br></code></pre></td></tr></table></figure>


                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E7%AC%94%E8%AE%B0/" class="category-chain-item">笔记</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E5%89%8D%E7%AB%AF/">#前端</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>JavaScript笔记（二）</div>
      <div>https://dl_coisini.gitee.io/2023/09/19/JavaScript笔记(二)/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>d-lue</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年9月19日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/09/25/TS%E5%9F%BA%E7%A1%80/" title="TS基础">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">TS基础</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/09/15/reactNote3/" title="React笔记（三）">
                        <span class="hidden-mobile">React笔记（三）</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body>
</html>
